package yogiro.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import yogiro.dao.TourInfoDAO;
import yogiro.to.CommentTO;
import yogiro.to.MapTO;
import yogiro.to.PictureTO;
import yogiro.to.RecommendTO;
import yogiro.to.TourInfoTO;
import yogiro.util.Utilities;

public class TourInfoService {
	private TourInfoDAO tourInfoDAO;
	public TourInfoService(TourInfoDAO tourInfoDAO){
		this.tourInfoDAO = tourInfoDAO;
	}
	
	public void insertTourInfo(TourInfoTO tio) throws Exception{
		String writeDate = Utilities.getNow();
		String c = Utilities.changeContentForDB(tio.getTourContent());
		tio.setTourWritedate(writeDate);
		tio.setTourContent(c);
		tourInfoDAO.insertTourInfo(tio);
		tio.setTourWritedate(Utilities.changeDayTimeFormat(writeDate));
	}
	
	public ArrayList<CommentTO> selectAllComment(int tourNo)throws SQLException{
		return tourInfoDAO.selectAllComment(tourNo);
	}
	
	public ArrayList<TourInfoTO> selectTourRecommend(String tourWriterType) throws Exception{
		return tourInfoDAO.selectTourRecommend(tourWriterType);
	}
	
	public PictureTO selectTourRecommend2(int pictureGroup) throws Exception{
		return tourInfoDAO.selectTourRecommend2(pictureGroup);
	}
	
	public ArrayList<PictureTO> selectTourRecommend3(int pictureGroup) throws Exception{
		return tourInfoDAO.selectTourRecommend3(pictureGroup);
	}
	
	public void pictureUp(PictureTO pto) throws Exception{
		tourInfoDAO.pictureUp(pto);
	}
	
	public ArrayList<TourInfoTO> selectTourListByPage(int page) throws Exception{
		return tourInfoDAO.selectTourListByPage(page);
	}
	
	public int selectTotalContent() throws SQLException{
		return tourInfoDAO.selectTotalContent();
	}
	
	public MapTO selectMapByTourNo(int mapGroup) throws SQLException{
		return tourInfoDAO.selectMapByTourNo(mapGroup);
	}
	
	public TourInfoTO getContentByTourNO(int tourNo) throws SQLException{
		//1. 조회수 증가
		tourInfoDAO.updateViewCount(tourNo);
		//2. no로 글 정보 조회
		TourInfoTO tio = tourInfoDAO.selectContentBytourNO(tourNo);
		//2-1 writedate를 yyyy.MM.dd HH:mm:ss 형식으로 변경
		tio.setTourWritedate(Utilities.changeDayTimeFormat(tio.getTourWritedate()));
		return tio;
	}
	
	public void insertMap(MapTO mto) throws Exception{
		tourInfoDAO.insertMap(mto);
	}
	
	public void insertRecommend(RecommendTO rto) throws Exception{
		tourInfoDAO.insertRecommend(rto);
	}
	public int selectRecommend(HashMap map) throws Exception{
		return tourInfoDAO.selectRecommend(map);
	}
	public int updateRecommend(int tourNo) throws Exception{
		return tourInfoDAO.updateRecommend(tourNo);
	}
	
	public int updateContent(TourInfoTO tio) throws Exception{
		tio.setTourContent(Utilities.changeContentForDB(tio.getTourContent()));
		tio.setTourWritedate(Utilities.getNow());
		return tourInfoDAO.updateContent(tio);
	}
	
	public int updatePicture(PictureTO pto) throws Exception{
		return tourInfoDAO.updatePicture(pto);
	}
	
	public int updatemap(MapTO mto) throws Exception{
		return tourInfoDAO.updateMap(mto);
	}
	
	public int deleteContentTourNO(int tourNo) throws Exception{
		return tourInfoDAO.deleteContentByTourNO(tourNo);
	}
	
	public int deletePictureTourNO(int pictureGroup) throws Exception{
		return tourInfoDAO.deletePictureByTourNO(pictureGroup);
	}
	
	public int deleteMapTourNO(int mapGroup) throws Exception{
		return tourInfoDAO.deleteMapByTourNO(mapGroup);
	}
	
	public int deleteCommentByTourNO(int tourNo) throws Exception{
		return tourInfoDAO.deleteCommentByTourNO(tourNo);
	}
	
}
